//
//  RCVisitorView.swift
//  RC-微博0305
//
//  Created by 任淳 on 16/3/6.
//  Copyright © 2016年 ReverChun. All rights reserved.
//

import UIKit
import SnapKit

//定义一个代理
// optional 还是OC中的关键字 如果在Swift中要使用OC中的一些机制 或者特性 需要在前加入@objc告诉编译器该声明是兼容OC的  func 前面加了东西optional 就要在方法前加@objc
@objc protocol RCVisitorViewDelegate:NSObjectProtocol {
    //可选方法 不写optional就是必须实现的方法 不实现就会报错
    optional func loginBtnWillPush()
    
    optional func regisiterBtnWillPush()
}

class RCVisitorView: UIView {

    //闭包方式
    var btnDidPushClosure:(()->())?
    
    
    //定义一个代理属性 
    //声明代理对象  代理的声明 在OC中必须使用 weak/assagin  为了防止循环引用
    weak var visitorDelegate:RCVisitorViewDelegate?
    
    
    //view的入口 最先执行的方法
    //在这里面创建view上的图片和按钮
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        self.backgroundColor = UIColor.whiteColor()
        
        //添加子控件
        addSubview(circleImage)
        //遮挡图片  要遮挡谁 就把谁添加在遮挡图片的下层(比遮挡图片先添加就可以遮挡)
        addSubview(hiddenImage)
        addSubview(houseImage)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        //设置约束
        setConstraints()
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        //fatal(致命的)  默认实 现报错  可以注销该默认报错的代码 让当前控件支持从xib / sb中加载
        fatalError("init(coder:) has not been implemented")
//        super.init(coder: aDecoder)
    }
    
    
    
    //不同控制器的circleImage和lab的设置
    //imageName可能为空 所以要定义成可选属性
    func setCircleImageAndLab(imageName name:String? ,labText title:String) {
        //分是不是首页
        if name == nil {
            //主页 ,就让circleImage转起来
            circleImageAnimation()
            
        }else {
            //非主页
            circleImage.image = UIImage(named: name!)
            tipLabel.text = title
            houseImage.hidden = true
//            hiddenImage.hidden = true
            bringSubviewToFront(circleImage)
        }
        
    }
    
    //circleImage旋转的方法
    private func circleImageAnimation() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.repeatCount = MAXFLOAT
        anim.duration = 10
        anim.toValue = 2 * M_PI
        //第二次进入主页发现图片不转了 被移除了 所以每次界面消失之后不移除动画
        //默认就是移除动画
        anim.removedOnCompletion = false
        circleImage.layer.addAnimation(anim, forKey: nil)
    }
    
    //登录和注册按钮的点击事件 点击方法要用private修饰 就必须加前缀@objc 不然找不到该方法
    //OC是运行时机制 而swift是编译时 找不到 private func loginBtnDidPush
    @objc private func loginBtnDidPush() {
        print("登录了")
        //点击后实现相应的代理方法
        visitorDelegate?.loginBtnWillPush?()
        
        //回调闭包
//        btnDidPushClosure?()
        
    }

    @objc private func regisiterBtnDidPush() {
        print("注册了")
        //点击后实现相应的代理方法
        visitorDelegate?.regisiterBtnWillPush?()
    }
    
//====================================================================================
    //设置约束
    private func setConstraints() {
    
        //circleImage
        circleImage.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
        }
        
        //遮挡图片
        hiddenImage.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleImage)
            
        }
        
        //houseImage
        houseImage.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleImage.snp_center)
            
        }
        
        //登录按钮的约束
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(circleImage.snp_centerX).offset(-70)
            make.centerY.equalTo(circleImage.snp_centerY).offset(170)
            make.width.equalTo(100)
            
        }
        
        //注册按钮的约束
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(circleImage.snp_centerX).offset(70)
            make.centerY.equalTo(circleImage.snp_centerY).offset(170)
            make.width.equalTo(100)
        }
        
        //label的约束
        tipLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(circleImage.snp_centerX)
            make.centerY.equalTo(circleImage.snp_centerY).offset(110)
            make.height.equalTo(40)
            make.width.equalTo(260)
        }
        
        
    }
 
//====================================================================================
    
    //懒加载view上的各个控件
    lazy var circleImage:UIImageView = {
       
        let icon = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return icon
        
    }()
    
    lazy var houseImage:UIImageView = {
       
        let icon = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return icon
    }()
    
    lazy var hiddenImage:UIImageView = {
       
        let image = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return image
    }()
    
    lazy var loginBtn:UIButton = {
       
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        btn.setTitle("登录", forState: .Normal)
        btn.setTitleColor(UIColor.grayColor(), forState: .Normal)
        btn.frame.size = CGSize.init(width: 80, height: 30)
        btn.sizeToFit()
        //登录的点击事件
       btn.addTarget(self, action: "loginBtnDidPush", forControlEvents: .TouchUpInside)
       
        return btn
    }()

    lazy var registerBtn:UIButton = {
        
        let btn = UIButton()
        
        btn.frame.size = CGSizeMake(80, 30)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        btn.setTitle("注册", forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(20)
        btn.sizeToFit()
        //注册的点击事件
        btn.addTarget(self, action: "regisiterBtnDidPush", forControlEvents: .TouchUpInside)
        return btn
        
    }()
    
    lazy var tipLabel: UILabel = {
        let l = UILabel()
        l.numberOfLines = 0
        l.text = "关注一些人,回到这里看看有什么惊喜"
        l.textColor = UIColor.darkGrayColor()
        l.font = UIFont.systemFontOfSize(14)
        l.textAlignment = .Center
        //自适应大小
        l.sizeToFit()
        
        return l
    }()

    
    
}

